var clock = new Halo.Clock();

function resizeUI(w, h) {
    var container = document.getElementById('container');

    if (container) {
        container.style.width = w + 'px';
        container.style.height = h + 'px';
    }

    var remUnit = h / 1334 * 100;
    var htmlStyle = document.styleSheets[0].rules[0];
    htmlStyle.style.fontSize = remUnit + "px";
}

function onResizeWindow() {
    var w = document.documentElement.clientWidth;
    var h = document.documentElement.clientHeight;

    resizeUI(w, h);

    Halo.Config.onResizeWindow(w, h);
}

function onResizeWindow() {
    var w = document.documentElement.clientWidth;
    var h = document.documentElement.clientHeight;

    Halo.Config.onResizeWindow(w, h);
}

function onMouseDown(evt) {
    var x = evt.clientX;
    var y = document.documentElement.clientHeight - evt.clientY;

    Halo.Config.scene() && Halo.Config.scene().handleEvent(evt, Halo.EVT_UI_DOWN, x, y);
}

function onMouseUp(evt) {
    var x = evt.clientX;
    var y = document.documentElement.clientHeight - evt.clientY;

    Halo.Config.scene() && Halo.Config.scene().handleEvent(evt, Halo.EVT_UI_UP, x, y);
}

function onMouseMove(evt) {
    var x = evt.clientX;
    var y = document.documentElement.clientHeight - evt.clientY;

    Halo.Config.scene() && Halo.Config.scene().handleEvent(evt, Halo.EVT_UI_Move, x, y);
}

var touch_identifier = null;

function onTouchStart(evt) {
    if (evt.changedTouches.length === 1) {
        var t = evt.changedTouches[0];
        var x = t.pageX;
        var y = document.documentElement.clientHeight - t.pageY;
        touch_identifier = t.identifier;
        Halo.Config.scene() && Halo.Config.scene().handleEvent(evt, Halo.EVT_UI_DOWN, x, y);
    } else {
        Halo.Config.scene() && Halo.Config.scene().handleEvent(evt);
    }
}

function onTouchEnd(evt) {
    if (evt.changedTouches.length === 1) {
        var t = evt.changedTouches[0];
        if (touch_identifier === t.identifier) {
            var x = t.pageX;
            var y = document.documentElement.clientHeight - t.pageY;
            Halo.Config.scene() && Halo.Config.scene().handleEvent(evt, Halo.EVT_UI_UP, x, y);
        }
    } else {
        Halo.Config.scene() && Halo.Config.scene().handleEvent(evt);
    }
}

function onTouchMove(evt) {
    if (evt.changedTouches.length === 1) {
        var t = evt.changedTouches[0];
        if (touch_identifier === t.identifier) {
            var x = t.pageX;
            var y = document.documentElement.clientHeight - t.pageY;
            Halo.Config.scene() && Halo.Config.scene().handleEvent(evt, Halo.EVT_UI_MOVE, x, y);
        }
    } else {
        Halo.Config.scene() && Halo.Config.scene().handleEvent(evt);
    }
}

function onKeyDown(evt) {
    Halo.Config.scene() && Halo.Config.scene().handleEvent(evt);
}

function onContextMenu(evt) {
    Halo.Config.scene() && Halo.Config.scene().handleEvent(evt);
}

function onMouseWheel(evt) {
    Halo.Config.scene() && Halo.Config.scene().handleEvent(evt);
}

function mainloop() {
    var dt = clock.getDelta();

    Halo.Config.update(dt);

    requestAnimationFrame(mainloop);
}

function setupListener() {
    window.addEventListener('keydown', onKeyDown, false);
    document.addEventListener('contextmenu', onContextMenu, false);
    document.addEventListener('wheel', onMouseWheel, false);
    if (!isAndroid) {
        document.addEventListener('mousedown', onMouseDown, false);
        document.addEventListener('mouseup', onMouseUp, false);
        document.addEventListener('mousemove', onMouseMove, false);
    }
    document.addEventListener('touchstart', onTouchStart, false);
    document.addEventListener('touchend', onTouchEnd, false);
    document.addEventListener('touchmove', onTouchMove, false);
}

Halo.Config.bDebug = true;

var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf('android') > -1 || ua.indexOf('adr') > -1;
var isWeixinBrowser = /micromessenger/.test(ua);

function init() {
    var w = document.documentElement.clientWidth;
    var h = document.documentElement.clientHeight;
    resizeUI(w, h);

    window.addEventListener('resize', onResizeWindow, false);

    if (window.bServerMode) {
        Halo.init(null, "/assets/", null, function () {
            var url = 'test.halo';

            var xhr = new XMLHttpRequest();
            xhr.open('GET', url, true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === XMLHttpRequest.DONE) {
                    if (xhr.status === 200) {
                        onLoaded(xhr.response);
                    } else {
                        Toast("error");
                    }
                } else {
                    //loading...
                }
            }

            xhr.responseType = 'arraybuffer';
            if (xhr.overrideMimeType) xhr.overrideMimeType('text/plain');
            xhr.send(null);

            xhr.onprogress = function (evt) {
                if (evt.lengthComputable) {
                    var percent = Math.floor(evt.loaded / evt.total * 100);
                    document.getElementById("percent").innerHTML = percent.toString() + '%';
                }
            };

            function onLoaded(data) {
                setupListener();

                var meta = Halo.ResourceManager.unzip(data);
                Halo.parseProject(meta, function (res) {
                    if (res === null) {
                        document.getElementById('container').remove();
                        Halo.Config.start(document.body);
                        Halo.Config.play();
                        onResizeWindow();
                        mainloop();
                    }
                });
            }
        });
    } else {
        Halo.init(null, project, desktopLoader, function () {
            setupListener()

            if (window.bPlayBatch) {
                Halo.Config.loadBatch('main', function (res) {
                    if (res === null) {
                        Halo.Config.start(document.body);
                        Halo.Config.play();
                        onResizeWindow();
                        mainloop();
                    }
                });
            } else {
                var list = getAllRes();
                Halo.ResourceManager.loadList(list, function (res) {
                    if (res === null) {
                        Halo.Config.start(document.body);
                        Halo.Config.play();
                        onResizeWindow();
                        mainloop();
                    }
                });
            }
        });
    }
}

if (isWeixinBrowser) {
    document.addEventListener("WeixinJSBridgeReady", function () {
        init();
    }, false);

} else {
    document.onreadystatechange = function () {
        if (document.readyState === "complete") {
            init();
        }
    }
}